package com.gientech.spring.web;

import com.gientech.spring.model.Coffee;
import com.gientech.spring.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
public class CoffeeController {

    @Autowired
    private CoffeeService coffeeService;

    @RequestMapping(value = "/coffee/", method = RequestMethod.GET)
    public ResponseEntity<List<Coffee>> listAllCoffee() {
        List<Coffee> coffeeList = coffeeService.findAll();
        if(coffeeList.isEmpty()){
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<List<Coffee>>(coffeeList, HttpStatus.OK);
    }

    @RequestMapping(value = "/coffee/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Coffee> getJsonCoffee(@PathVariable("id") String id) {
        System.out.println("Fetching coffee with id " + id);
        Coffee coffee = coffeeService.findById(Long.valueOf(id));
        if (null == coffee) {
            System.out.println("coffee with id " + id + " not found");
            return new ResponseEntity<Coffee>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Coffee>(coffee, HttpStatus.OK);
    }

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_ATOM_XML_VALUE)
    public ResponseEntity<Coffee> getXmlCoffee(@PathVariable("id") String id) {
        System.out.println("Fetching coffee with id " + id);
        Coffee coffee = coffeeService.findById(Long.valueOf(id));
        if (null == coffee) {
            System.out.println("coffee with id " + id + " not found");
            return new ResponseEntity<Coffee>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Coffee>(coffee, HttpStatus.OK);
    }

}
